
class RapidsharePlugin:
	def __init__(self):
		# put all the links this plugins supports here - it will be used to filter supported URLs
		self.supported_sites = ['rapidshare.com']
		self.supported_urls = ['//rapidshare.com', 'www.rapidshare.com']

	def get(self, url):
		"""Just get a file from rapidshare"""
		self._auth_rapidshare()

		local_file = get_filename_from_url(url)
		download_file_with_retries(local_file, url)
		config.report.report_finish(local_file)

	def url_matches(self, url):
		"""Check whether we support this URL"""
		for l in self.supported_urls:
			if url.find(l) != -1:
				return True
		return False

	def version(self):
		"""Return plugin version - when interface changes, the version changes"""
		return 1

	def _auth_rapidshare(self):
		"""Authenticate with rapidshare.com, so we can download freely afterwards"""
		CookiesLoaded()
		if not cookie_present('rapidshare.com'):
			if (not config.rs_user) or (not config.rs_passwd):
				raise NoAuth("Need both rapidshare username and password to D/L from rapidshare")

			post_data = urllib.urlencode({'login': config.rs_user, 'password': config.rs_passwd})
			url = 'https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi'
			for line in urllib2.urlopen(url, post_data).read().splitlines():
				if line.find('E#1') != -1:
					raise NoAuth("Premium-Account not found at RapidShare.com")
				elif line.find('E#2') != -1:
					raise NoAuth("Account found, but password is incorrect")

			config.cookies.save()
			config.cookies_loaded = True
			if not cookie_present('rapidshare.com'):
				raise NoAuth("Unknown error while authenticating rapdishare premium account")

modules = [RapidsharePlugin()]
